/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "registry.h"
#ifdef IPHONE_VERSION
#include <CoreFoundation/CoreFoundation.h>
#endif

namespace liba
{
namespace win32
{
namespace registry
{


bool write(const std::string & key, const std::string & value, const std::string & data)
{
	std::string path = key + value;
	CFStringRef fs_key = CFStringCreateWithCString( 0, path.c_str(), kCFStringEncodingWindowsLatin1 );
	CFStringRef fs_value = CFStringCreateWithCString( 0, data.c_str(), kCFStringEncodingWindowsLatin1 );

	// Set up the preference.
	CFPreferencesSetAppValue( fs_key, fs_value, kCFPreferencesCurrentApplication );

	// Write out the preference data.
	CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
	
	CFRelease( fs_key );
	CFRelease( fs_value );
	return true;
}

bool read(const std::string & key, const std::string & value, std::string * data)
{
	std::string path = key + value;
	CFStringRef fs_key = CFStringCreateWithCString( 0, path.c_str(), kCFStringEncodingWindowsLatin1 );

	// Read the preference.
	CFStringRef fs_val = (CFStringRef)CFPreferencesCopyAppValue( fs_key, kCFPreferencesCurrentApplication );
	CFRelease( fs_key );
	if( !fs_val )
		return false;

	//kCFStringEncodingWindowsLatin1
	//kCFStringEncodingASCII
	const char * char_data = CFStringGetCStringPtr( fs_val, kCFStringEncodingWindowsLatin1 );
	if( char_data )
	{
		*data = char_data;
		CFRelease( fs_val );
		return true;
	}

	char buffer[2048];
	if( !CFStringGetCString( fs_val, buffer, 2048, kCFStringEncodingWindowsLatin1 ) )
	{
		CFRelease( fs_val );
		return false;
	}

	*data = buffer;
	CFRelease( fs_val );
	return true;
}

bool write(const std::string & key, const std::string & value, long number)
{
	std::string path = key + value;
	CFStringRef fs_key = CFStringCreateWithCString( 0, path.c_str(), kCFStringEncodingWindowsLatin1 );
	CFNumberRef fs_value = CFNumberCreate( 0, kCFNumberIntType, &number );

	// Set up the preference.
	CFPreferencesSetAppValue( fs_key, fs_value, kCFPreferencesCurrentApplication );

	// Write out the preference data.
	CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
	CFRelease( fs_key );
	return true;
}

bool read(const std::string & key, const std::string & value, long * number)
{
	std::string path = key + value;
	CFStringRef fs_key = CFStringCreateWithCString( 0, path.c_str(), kCFStringEncodingWindowsLatin1 );
	// Read the preference.
	CFNumberRef fs_val = (CFNumberRef)CFPreferencesCopyAppValue( fs_key, kCFPreferencesCurrentApplication );
	CFRelease( fs_key );
	if( !fs_val )
		return false;

	if( !CFNumberGetValue( fs_val, kCFNumberIntType, number ) )
	{
		CFRelease( fs_val );
		return false;
	}

	CFRelease( fs_val );
	return true;
}

bool delete_key(const std::string & key)
{
	return false;
}

bool delete_value(const std::string & key, const std::string & value)
{
	std::string path = key + value;
	CFStringRef fs_key = CFStringCreateWithCString( 0, path.c_str(), kCFStringEncodingWindowsLatin1 );

	CFPreferencesSetAppValue( fs_key, 0, kCFPreferencesCurrentApplication );
	CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
	CFRelease( fs_key );
	return true;
}

} // namespace registry
} // namespace win32
} // namespace liba

